package com.webber.demo.common.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

/**
 * 时间工具类--时间获取、解析、时间转字符
 * 
 * @author mhl
 * 
 */
public class DateUtil {
	/**
	 * yyyy-MM-dd
	 */
	public static final String YYYY_MM_DD_PATTERN = "yyyy-MM-dd";
	/**
	 * yyyyMMdd
	 */
	public static final String YYYYMMDDPATTERN = "yyyyMMdd";
	/**
	 * yyyy-MM-dd HH:mm:ss
	 */
	public static final String YYYY_MM_DD_HH_MM_SS_PATTERN = "yyyy-MM-dd HH:mm:ss";
	/**
	 * yyyyMMddHHmmss
	 */
	public static final String YYYYMMDDHHMMSSPATTERN = "yyyyMMddHHmmss";
	/**
	 * yyyyMMddHHmmsssss
	 */
	public static final String YYYY_MM_DD_HH_MM_SS_SSS_PATTERN = "yyyyMMddHHmmsssss";
	/**
	 * HH:mm:ss
	 */
	public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
	/**
	 * MONTH
	 */
	public static final String DEFAULT_MONTH = "MONTH";
	/**
	 * YEAR
	 */
	public static final String DEFAULT_YEAR = "YEAR";
	/**
	 * DAY
	 */
	public static final String DEFAULT_DATE = "DAY";
	/**
	 * HOUR
	 */
	public static final String DEFAULT_HOUR = "HOUR";
	/**
	 * MINUTE
	 */
	public static final String DEFAULT_MINUTE = "MINUTE";
	/**
	 * SECOND
	 */
	public static final String DEFAULT_SECOND = "SECOND";
	/**
	 * yyyy-MM-dd HH-mm
	 */
	public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH-mm";
	/**
	 * "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"
	 */
	public static final String[] WEEKS = { "星期日", "星期一", "星期二", "星期三", "星期四","星期五", "星期六" };

	/**
	 * 日历年份 java.util.Calendar.YEAR = 1 [0x1]
	 */
	public static final int YEAR_CALENDAR = Calendar.YEAR;
	/**
	 * 月份 java.util.Calendar.MONTH = 2 [0x2]
	 */
	public static final int MONTH_CALENDAR = Calendar.MONTH;
	/**
	 * 日 java.util.Calendar.DATE = 5 [0x5]
	 */
	public static final int DATE_CALENDAR = Calendar.DATE;
	/**
	 * 小时 java.util.Calendar.HOUR = 10 [0xa]
	 */
	public static final int HOUR_CALENDAR = Calendar.HOUR;
	/**
	 * 分钟 java.util.Calendar.MINUTE = 12 [0xc]
	 */
	public static final int MINUTE_CALENDAR = Calendar.MINUTE;
	/**
	 * 秒 java.util.Calendar.SECOND = 13 [0xd]
	 */
	public static final int SECOND_CALENDAR = Calendar.SECOND;
	/**
	 * 星期 java.util.Calendar.DAY_OF_WEEK = 7 [0x7]
	 */
	public static final int DAY_OF_WEEK_CALENDAR = Calendar.DAY_OF_WEEK;
	private static final String YYYY_MM_DD_HH_MM = null;

	/**
	 * 获取系统当前时间（秒）yyyy-MM-dd HH:mm:ss
	 * 
	 * @author mhl
	 * @return
	 */
	public static Timestamp getTime() {
		SimpleDateFormat myFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS_PATTERN);
		Calendar calendar = Calendar.getInstance();
		String mystrdate = myFormat.format(calendar.getTime());
		return Timestamp.valueOf(mystrdate);
	}

	/**
	 * 获取当前时间
	 * 
	 * @author mhl
	 * @return
	 */
	public static String getCurrentTime(String format) {
		String returnStr = null;
		SimpleDateFormat f = new SimpleDateFormat(format);
		Date date = new Date();
		returnStr = f.format(date);
		return returnStr;
	}

	/**
	 * 解析时间
	 * 
	 * @author mhl
	 * @return
	 */
	public static Date parseDate(String strDate) {
		if (StringUtils.isBlank(strDate)) {
			return null;
		}
		SimpleDateFormat sdf = null;
		Date date = null;
		try {
			sdf = new SimpleDateFormat("yyyy-MM-dd");
			date = sdf.parse(strDate);
		} catch (Exception e) {
			return null;
		}
		return date;
	}

	/**
	 * 
	 * <p>
	 * Title: parseDate
	 * </p>
	 * <p>
	 * Description:String转date
	 * </p>
	 * 
	 * @param strDate
	 * @param reg
	 * @return
	 * @author JobsZhang
	 * @date 2017年8月29日 下午4:32:03
	 */
	public static Date parseDate(String strDate, String reg) {
		if (StringUtils.isBlank(strDate)) {
			return null;
		}
		SimpleDateFormat sdf = null;
		Date date = null;
		try {
			sdf = new SimpleDateFormat(reg);
			date = sdf.parse(strDate);
		} catch (Exception e) {
			return null;
		}
		return date;
	}

	/**
	 * 时间类型转字符串
	 * 
	 * @author mhl
	 * @return
	 */
	public static String date2String(Date date) {
		return date2String(date, YYYY_MM_DD_HH_MM);
	}

	public static String date2String(Date date,String reg) {
		if (null == date||!StringUtil.notBlank(reg)) {
			return "";
		}
		SimpleDateFormat sdf = null;
		sdf = new SimpleDateFormat(reg);
		return sdf.format(date);
	}
	/**
	 * time类型转字符串
	 * 
	 * @author mhl
	 * @return
	 */
	public static String time2String(Date date) {
		return date2String(date, YYYY_MM_DD_HH_MM_SS_PATTERN);
	}

	/**
	 * 是否是闰年
	 * 
	 * @param yearStr
	 * @return
	 */
	public static boolean IsLeapYear(String yearStr) {
		boolean flag = false;
		if ((null != yearStr) && (yearStr.length() != 0)) {
			int year = Integer.parseInt(yearStr);
			if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
				flag = true;
			} else {
				flag = false;
			}
		}
		return flag;
	}

	/**
	 * 获取每个月的最后一天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getLastDayOfMonth(String year, String month) {
		Calendar cal = Calendar.getInstance();

		cal.set(1, Integer.parseInt(year));
		cal.set(2, Integer.parseInt(month));
		cal.set(5, 1);
		cal.add(5, -1);
		Date lastDate = cal.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdf.format(lastDate);
		return sdf.format(lastDate);
	}

	/**
	 * 获取每个月的第一天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getFirstDayOfMonth(String year, String month) {
		Calendar cal = Calendar.getInstance();
		cal.set(1, Integer.parseInt(year));
		cal.set(2, Integer.parseInt(month));
		cal.set(5, 1);
		cal.add(5, -1);
		cal.set(5, 1);
		Date firstDate = cal.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdf.format(firstDate);
		return sdf.format(firstDate);
	}

	/**
	 * Timestamp转String
	 * 
	 * @author JobsZhang
	 * @param timestamp
	 * @return
	 */
	public static String Timestamp2String(Timestamp timestamp) {
		if (timestamp == null) {
			return null;
		}
		SimpleDateFormat df = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS_PATTERN);// 定义格式，不显示毫秒
		return df.format(timestamp);
	}

	/**
	 * Timestamp转String
	 * 
	 * @author JobsZhang
	 * @param timestamp
	 * @param reg
	 *            正则
	 * @return
	 */
	public static String Timestamp2String(Timestamp timestamp, String reg) {
		if (timestamp == null) {
			return null;
		}
		SimpleDateFormat df = new SimpleDateFormat(reg);// 定义格式，不显示毫秒
		return df.format(timestamp);
	}

	/**
	 * String转Timestamp
	 * 
	 * @author JobsZhang
	 * @param time
	 * @return
	 */
	public static Timestamp String2Timestamp(String time, String reg) {
		if (!isTimeLegal(time)) {
			return null;
		}
		SimpleDateFormat format1 = new SimpleDateFormat(reg);
		java.util.Date date;
		try {
			date = format1.parse(time.trim());
		} catch (ParseException e) {
			return null;
		}
		SimpleDateFormat format2 = new SimpleDateFormat(
				DateUtil.YYYY_MM_DD_HH_MM_SS_PATTERN);
		String sdate = format2.format(date);
		Timestamp fTimestamp = Timestamp.valueOf(sdate);
		return fTimestamp;

	}

	/**
	 * 判断字符串是否满足时间格式 ： yyyy-MM-dd HH:mm:ss
	 * 
	 * @author JobsZhang
	 * @param time
	 *            要判断的字符串
	 * @return 合法返回 true ; 不合法返回false
	 */
	public static boolean isTimeLegal(String time) {
		if (time == null && "".equals(time.trim())) {
			return false;
		}
		Pattern a = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s((([0-1][0-9])|(2?[0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
		Matcher b = a.matcher(time.trim());
		if (b.matches()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断字符串是否满足时间格式 ： yyyy-MM-dd
	 * 
	 * @author JobsZhang
	 * @param time
	 *            要判断的字符串
	 * @return 合法返回 true ; 不合法返回false
	 */
	public static boolean isDateLegal(String date) {
		if (date == null && "".equals(date.trim())) {
			return false;
		}
		Pattern a = Pattern.compile("(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[13579][26])00))-02-29)");
		Matcher b = a.matcher(date.trim());
		if (b.matches()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取需要的时间类型，可以获取年份，月份，小时，分钟
	 * 
	 * @param Type
	 *            类型
	 * @return
	 */
	public static int getCourentNeedType(int Type) {
		Calendar ca = Calendar.getInstance();
		return ca.get(Type);
	}

    /**
     * 判断某一时间是否在一个区间内
     *
     * @param sourceTime 时间区间,半闭合,如[10:00-20:00)
     * @param curTime    需要判断的时间 如10:00
     * @return
     * @throws IllegalArgumentException
     */
    public static boolean isInTime(String sourceTime, String curTime) {
        if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }
        if (curTime == null || !curTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + curTime);
        }
        String[] args = sourceTime.split("-");
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        try {
            long now = sdf.parse(curTime).getTime();
            long start = sdf.parse(args[0]).getTime();
            long end = sdf.parse(args[1]).getTime();
            if (args[1].equals("00:00")) {
                args[1] = "24:00";
            }
            if (end < start) {
                if (now >= end && now < start) {
                    return false;
                } else {
                    return true;
                }
            } else {
                if (now >= start && now < end) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }

    }
    /**
     * 判断两个日期大小，返回 endDate-startDate的时间戳
     *
     * @param endDate
     * @param startDate
     * @param reg       格式化字符串
     * @return
     */
    public static long distenceDate(String endDate, String startDate, String reg) {
        if (startDate == null || startDate.equals("") || endDate == null || endDate.equals("") || reg == null || reg.equals("")) {
            return 0;
        }
        DateFormat df = new SimpleDateFormat(reg);
        Date dt1 = null;
        Date dt2 = null;
        try {
            dt1 = df.parse(startDate);
            dt2 = df.parse(endDate);
            return dt2.getTime() - dt1.getTime();
        } catch (ParseException e) {
            return 0;
        }

    }
    /**
     * 判断传入时间距离当前时间几天
     *
     * @param enddate
     * @return
     */
    public static long hasday(String enddate) throws ParseException {
        if (enddate == null || "".equals(enddate))
            return 0;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1 = df.parse(enddate);
        long d = d1.getTime() - new Date().getTime();
        if (d > 0) {
            long daynum = 0;
            if (d > (24 * 60 * 60 * 1000)) {
                daynum = d / (24 * 60 * 60 * 1000);
                if ((d / (1000 * 60 * 60) - daynum * 24) > 0) {
                    daynum += 1;
                }
            } else {
                daynum = 1;
            }
            return daynum;
        } else {
            return 0;
        }
    }
    /**
     * 日期转换字符串
     *
     * @param date
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String dateTostr(Date date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (date != null) {
            return df.format(date);
        } else {
            return "";
        }
    }

    /**
     * 当前时间的前i天,格式：yyyy-MM-dd
     *
     * @return
     */
    public static String getDateStr(int i) {
        Date date = DateUtils.addDays(new Date(), -i);
        return DateFormatUtils.format(date, "yyyy-MM-dd");
    }

    /**
     * 当前时间的前i天,格式：yyyy-MM-dd
     *
     * @return
     */
    public static Date getDate(int i) {
        try {
            return DateUtils.parseDate(getDateStr(i), new String[]{"yyyy-MM-dd"});
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            return null;
        }
    }

    /**
     * 根据日期取得星期几
     * 对于创建SimpleDateFormat传入的参数：EEEE代表星期，如“星期四”；MMMM代表中文月份，如“十一月”；MM代表月份，如“11”
     *
     * @param date
     * @return
     */
    public static String getWeek(Date date, String reg) {
        if (reg != "EEEE" || date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(reg);
        String week = sdf.format(date);
        return week;
    }

    /**
     * 根据日期取得星期几
     * 对于创建SimpleDateFormat传入的参数：EEEE代表星期，如“星期四”；MMMM代表中文月份，如“十一月”；MM代表月份，如“11”
     *
     * @param date
     * @return
     */
    public static String getWeek(String date, String reg) {
        if (!"EEEE".equals(reg)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(reg);
        String week = null;
        try {
            week = sdf.format(new SimpleDateFormat("yyyy-MM-dd").parse(date));
        } catch (ParseException e) {
            return null;
        }
        return week;
    }

    /**
     * 根据日期取得星期几
     *
     * @param date
     * @return
     */
    public static String getWeek(Date date) {
        if (date == null) {
            return null;
        }
        String[] weeks = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return weeks[week_index];
    }

    /**
     * 根据日期取得星期几
     *
     * @param date
     * @return
     */
    public static String getWeek(String date) {
        if ("".equals(date) || date == null) {
            return null;
        }
        String[] weeks = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(date));
        } catch (ParseException e) {
            return null;
        }
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return weeks[week_index];
    }

    /**
     * 根据日期取得星期几
     *
     * @param date
     * @return
     */
    //zzp
    public static String getWeekChinese(String date) {
        if (date == null) {
            return null;
        }
        String[] weeks = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(date));
        } catch (ParseException e) {
            return null;
        }
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return weeks[week_index];
    }

    /**
     * 日期字符串转为日期格式,格式：yyyy-MM-dd
     *
     * @return
     */
    public static Date string2Date(String dateStr,String reg) {
        try {
            return DateUtils.parseDate(dateStr, new String[]{reg});
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获得时间字符串对应的  年，月，日
     * @param datestr  时间字符串
     * @param type yyyy年，MM 月,dd 日
     * @return
     */
    public static String getDateByType(String datestr,String type){
    	String result = "";
    	if(datestr!=null && !"".equals(datestr)){
	    	Date date = string2Date(datestr,YYYY_MM_DD_PATTERN);
	    	date= date==null?new Date():date;
	    	Calendar cal = Calendar.getInstance();
	    	cal.setTime(date);
	    	if("yyyy".equals(type)){
	    		result = String.valueOf(cal.get(Calendar.YEAR)); 
	    	}else if("MM".equals(type)){
	    		result = String.valueOf(cal.get(Calendar.MONTH)+1);
	    	}else if("dd".equals(type)){
	    		result = String.valueOf(cal.get(Calendar.DATE));
	    	}
    	}
    	return result;
    }

    /**
     * 获取月份 从1开始  1-12
     *
     * @param date
     * @return
     */
    //zzp
    public static int getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取日期   2012-12-14  获取到日期是14
     *
     * @param date
     * @return
     */
    //zzp
    public static int getDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DATE);
    }

    /**
     * @param dateStr
     * @return
     */
    //zzp
    public static Date getDates(String dateStr, String reg) {
        try {
            return DateUtils.parseDate(dateStr, new String[]{reg});
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当前时间往后14天的日期
     *
     * @return
     */
    //zzp
    public static List<String> get14Days() {
        int[] days = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
        List<String> returnData = new ArrayList<String>();
        for (int i : days) {
            try {
                returnData.add(getStatetime(i));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return returnData;
    }

    /**
     * 返回相对于当前时间的日期
     *
     * @return
     * @throws ParseException
     */
    //zzp
    public static String getStatetime(int num) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, num);
        Date monday = c.getTime();
        String preMonday = sdf.format(monday);
        return preMonday;
    }

    /**
     * 判断date1是否比date2大
     *
     * @param date1
     * @param date2
     * @return 1 date1>date2 2 date1<date2 0 date1==date2
     */
    public static int compareDate(String date1, String date2) {
        if ("".equals(date1)) {
            return 2;
        }
        if ("".equals(date2)) {
            return 1;
        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date11 = df.parse(date1);
            Date date22 = df.parse(date2);
            if (date11.getTime() > date22.getTime()) {
                return 1;
            } else if (date11.getTime() < date22.getTime()) {
                return 2;
            } else {
                return 0;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 1;
    }

    /**
     * 判断date1是否比date2大
     *
     * @param date1
     * @param date2
     * @return 1 date1>date2 2 date1<date2 0 date1==date2
     */
    public static int compareDate(Date date1, Date date2) {
        if (date1.getTime() > date2.getTime()) {
            return 1;
        } else if (date1.getTime() < date2.getTime()) {
            return 2;
        } else {
            return 0;
        }
    }

    /**
     * 将yyyy-MM-dd HH:mm:ss截取为yyyy-MM-dd
     *
     * @param dateStr
     * @return
     */
    public static String subDateString(String dateStr) {
        if (dateStr != null && dateStr.length() == 19) {
            return dateStr.substring(0, 10);
        }
        return dateStr;
    }
    /**
     * 时间转换 yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String dateTostrNoHMS(Date date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        if (date != null) {
            return df.format(date);
        } else {
            return "";
        }
    }
    
    /**
     * 判断两个时间段有无交集
     * reg=yyyy-MM-dd HH:mm
     *
     * @param startdate1 yyyy-MM-dd HH:mm
     * @param enddate1   yyyy-MM-dd HH:mm
     * @param startdate2 yyyy-MM-dd HH:mm
     * @param enddate2   yyyy-MM-dd HH:mm
     * @return
     */
    public static boolean checkTimes(String startdate1, String enddate1, String startdate2, String enddate2) {
        if (startdate1 != null && !startdate1.equals("") && enddate1 != null && !enddate1.equals("") && startdate2 != null && !startdate2.equals("") && enddate2 != null && !enddate2.equals("")) {
            return checkTimes(startdate1, enddate1, startdate2, enddate2, "yyyy-MM-dd HH:mm");
        }
        return false;
    }

    /**
     * 判断两个时间段有无交集
     *
     * @param startdate1
     * @param enddate1
     * @param startdate2
     * @param enddate2
     * @return
     */
    public static boolean checkTimes(String startdate1, String enddate1, String startdate2, String enddate2, String reg) {
        SimpleDateFormat format = new SimpleDateFormat(reg);
        Date leftStartDate = null;
        Date leftEndDate = null;
        Date rightStartDate = null;
        Date rightEndDate = null;
        try {
            leftStartDate = format.parse(startdate1);
            leftEndDate = format.parse(enddate1);
            rightStartDate = format.parse(startdate2);
            rightEndDate = format.parse(enddate2);
        } catch (ParseException e) {
            return false;
        }

        return
                ((leftStartDate.getTime() >= rightStartDate.getTime())
                        && leftStartDate.getTime() < rightEndDate.getTime())
                        ||
                        ((leftStartDate.getTime() > rightStartDate.getTime())
                                && leftStartDate.getTime() <= rightEndDate.getTime())
                        ||
                        ((rightStartDate.getTime() >= leftStartDate.getTime())
                                && rightStartDate.getTime() < leftEndDate.getTime())
                        ||
                        ((rightStartDate.getTime() > leftStartDate.getTime())
                                && rightStartDate.getTime() <= leftEndDate.getTime());
    }
    
    /**
     * 判断时间是否在时间段内
     *
     * @param date         当前时间 yyyy-MM-dd HH:mm:ss
     * @param strDateBegin 开始时间 00:00
     * @param strDateEnd   结束时间 00:05
     * @return
     */
    public static boolean isInDate(Date date, String strDateBegin, String strDateEnd) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = sdf.format(date);
        // 截取当前时间时分秒
        int strDateH = Integer.parseInt(strDate.substring(11, 13));
        int strDateM = Integer.parseInt(strDate.substring(14, 16));
        // 截取开始时间时分秒
        int strDateBeginH = Integer.parseInt(strDateBegin.substring(0, 2));
        int strDateBeginM = Integer.parseInt(strDateBegin.substring(3, 5));
        // 截取结束时间时分秒
        int strDateEndH = Integer.parseInt(strDateEnd.substring(0, 2));
        int strDateEndM = Integer.parseInt(strDateEnd.substring(3, 5));
        if ((strDateH >= strDateBeginH && strDateH <= strDateEndH)) {
            // 当前时间小时数在开始时间和结束时间小时数之间
            if (strDateH > strDateBeginH && strDateH < strDateEndH) {
                return true;
                // 当前时间小时数等于开始时间小时数，分钟数在开始和结束之间
            } else if (strDateH == strDateBeginH && strDateM >= strDateBeginM
                    && strDateM <= strDateEndM) {
                return true;
                // 当前时间小时数等于开始时间小时数，分钟数等于开始时间分钟数，秒数在开始和结束之间
            } else if (strDateH == strDateBeginH && strDateM == strDateBeginM) {
                return true;
            }
            // 当前时间小时数大等于开始时间小时数，等于结束时间小时数，分钟数小等于结束时间分钟数
            else if (strDateH >= strDateBeginH && strDateH == strDateEndH
                    && strDateM <= strDateEndM) {
                return true;
                // 当前时间小时数大等于开始时间小时数，等于结束时间小时数，分钟数等于结束时间分钟数，秒数小等于结束时间秒数
            } else if (strDateH >= strDateBeginH && strDateH == strDateEndH && strDateM == strDateEndM) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    /**
     * 
     * <p>Title: getTimeStr</p>
     * <p>Description: 获取当前日期</p>
     * @return 返回格式YYYYMMDD
     * @author JobsZhang
     * @date 2017年10月13日 下午5:11:35
     */
    public static String getTimeStr(){
    	return getTimeStr(YYYYMMDDPATTERN);
    }
    
    public static String getTimeStr(String reg){
    	return date2String(new Date(),reg);
    }
	public static void main(String[] args) {
		System.out.println(isDateLegal("2015-02-12"));
		System.out.println(isTimeLegal("2014-12-12 00:00:00"));
		System.out.println(String2Timestamp("2014-12-12 00:00:00",YYYY_MM_DD_HH_MM_SS_PATTERN));
		System.out.println(getCourentNeedType(YEAR_CALENDAR));
		System.out.println(Timestamp2String(new Timestamp(2017, 05, 01, 12, 12,	0, 0), "yyyy-MM-dd"));
		System.out.println(getTimeStr());
	}
}
